package com.shenzhen.start.client;

import com.shenzhen.start.common.JSONSerializer;
import com.shenzhen.start.common.RpcEncoder;
import com.shenzhen.start.common.RpcRequest;
import com.shenzhen.start.handler.UserClientHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class RPCConsumer {

    //1、创建一个线程对象 -- 他要处理自定义事件
    private static ExecutorService executorService =
            Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

    //2、声明一个自定义事件处理器

    private static UserClientHandler userClientHandler;

    //3.编写方法,初始化客户端  ( 创建连接池  bootStrap  设置bootstrap  连接服务器)
    public static void initClient(){
        //初始化 UserClientHandler
        userClientHandler = new UserClientHandler();

        //创建连接池对像
        EventLoopGroup group = new NioEventLoopGroup();

        //创建客户端引导对象
        Bootstrap bootstrap = new Bootstrap();

        //设置bootstrap
        bootstrap.group(group)
                //设置通道为nio
                .channel(NioSocketChannel.class)
                //设置请求协议为TCP
                .option(ChannelOption.TCP_NODELAY,true)
                //监听channel并初始化
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        //获取ChannelPipeline
                        ChannelPipeline channelPipeline = socketChannel.pipeline();
                        //设置编码
                        //有客户端向服务端写数据，添加编码器
                        channelPipeline.addLast(new RpcEncoder(RpcRequest.class, new JSONSerializer()));
                        //接收服务端返回数据“success"，添加解码器
                        channelPipeline.addLast(new StringDecoder());
                        //添加自定义事件处理器
                        channelPipeline.addLast(userClientHandler);
                    }
                });
        //连接服务器
        try {
            bootstrap.connect("127.0.0.1",8999).sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //4.编写一个方法,使用JDK的动态代理创建对象
    // serviceClass 接口类型,根据哪个接口生成子类代理对象;   providerParam :  "UserService#sayHello#"
    public static Object createProxy(Class<?> serviceClass){
        return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
                new Class[]{serviceClass}, new InvocationHandler() {
                    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                        //初始化客户端cliet
                        if(userClientHandler == null){
                            initClient();
                        }
                        //给UserClientHandler 设置请求对象
                        RpcRequest rpcRequest = new RpcRequest();
                        rpcRequest.setRequestId(UUID.randomUUID().toString());
                        rpcRequest.setClassName(method.getDeclaringClass().getName());
                        rpcRequest.setMethodName(method.getName());
                        rpcRequest.setParameterTypes(method.getParameterTypes());
                        rpcRequest.setParameters(objects);
                        System.out.println("请求内容："+rpcRequest);
                        //将请求对象客户端处理器
                        userClientHandler.setRpcRequest(rpcRequest);

                        //使用线程池,开启一个线程处理处理call() 写操作,并返回结果
                        Object result = executorService.submit(userClientHandler).get();
                        //return 结果
                        return result;
                    }
                });
    }
}
